home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / programming / e / lsestuff / ipc.e < prev    next >
Text File  |  1999-11-29  |  4KB  |  186 lines

  1. OPT MODULE
  2.  
  3. MODULE 'exec/ports'
  4. MODULE 'exec/nodes'
  5. MODULE 'dos/dos'
  6.  
  7. EXPORT OBJECT waitSigs
  8.    PRIVATE
  9.    alloced_sigs
  10.    added_sigs
  11.    received_sigs
  12. ENDOBJECT
  13.  
  14. EXPORT OBJECT newPort
  15.    PRIVATE
  16.    mp:PTR TO mp
  17.    active:INT
  18. ENDOBJECT
  19.  
  20. OBJECT rajraj
  21.    private:mn
  22. ENDOBJECT
  23.  
  24. OBJECT newMsg OF rajraj
  25.    PRIVATE
  26.    sendertask
  27.    type
  28.    cmnd
  29.    data
  30.    reply
  31. ENDOBJECT
  32.  
  33. PROC getType() OF newMsg IS self.type
  34.  
  35. PROC getCmnd() OF newMsg IS self.cmnd
  36.  
  37. PROC getData() OF newMsg IS self.data
  38.  
  39. PROC do(newPort:PTR TO newPort, type=NIL, cmnd=NIL, data=NIL) OF newMsg
  40.    self.private.ln.type := NT_MESSAGE
  41.    self.private.length := SIZEOF newMsg
  42.    self.type := IF type = NIL THEN self.sendertask ELSE type
  43.    self.cmnd := cmnd
  44.    self.data := data
  45.    IF newPort.isActive() = FALSE THEN RETURN -1
  46.    PutMsg(newPort.mp, self)
  47.    Wait(SIGBREAKF_CTRL_F)
  48. ENDPROC self.reply
  49.  
  50. PROC doName(portname, type=NIL, cmnd=NIL, data=NIL) OF newMsg
  51.    DEF newPort:newPort
  52.    newPort.mp := FindPort(portname)
  53.    IF newPort.mp THEN self.do(newPort, type, cmnd, data)
  54. ENDPROC newPort.mp
  55.  
  56. PROC reply(reply=NIL) OF newMsg
  57.    self.reply := reply
  58.    Signal(self.sendertask, SIGBREAKF_CTRL_F)
  59. ENDPROC
  60.  
  61. PROC newMsg() OF newMsg
  62.    self.sendertask:=FindTask(0)
  63. ENDPROC
  64.  
  65. PROC getReply() OF newMsg IS self.reply
  66.  
  67. PROC end() OF newMsg IS NIL
  68.  
  69. ->----------NEWMSG---------
  70.  
  71. /* msg TYPES */
  72. EXPORT CONST NMT_CTRL=-10, NMT_STREAM=-20, NMT_MISC=-30, NMT_ERRORS=-40
  73.  
  74. EXPORT PROC doMsgQ(newPort, type=NIL, command=NIL, data=NIL)
  75.    DEF msg:PTR TO newMsg, er
  76.    NEW msg.newMsg()
  77.    er := msg.do(newPort, type, command, data)
  78.    END msg
  79. ENDPROC er
  80.  
  81.  
  82. ->-------NEWPORT------------------
  83.  
  84. PROC newPort(name=NIL, pri=NIL) OF newPort
  85.    self.mp := CreateMsgPort()
  86.    IF self.mp = NIL THEN RETURN NIL
  87.    self.mp.ln.name := name
  88.    self.mp.ln.pri := pri
  89.    IF name THEN AddPort(self.mp)
  90.    self.active := TRUE
  91. ENDPROC self.mp
  92.  
  93. PROC end() OF newPort
  94.    self.active := FALSE
  95.    IF self.mp.ln.name THEN RemPort(self.mp)
  96.    self.clear()
  97.    DeleteMsgPort(self.mp)
  98. ENDPROC
  99.  
  100. PROC mp() OF newPort IS self.mp
  101.  
  102. PROC clear() OF newPort
  103.    DEF newMsg:PTR TO newMsg
  104.    WHILE newMsg:=self.collect()
  105.       IF newMsg THEN newMsg.reply(NIL)
  106.    ENDWHILE
  107. ENDPROC
  108.  
  109. PROC isActive() OF newPort IS self.active
  110.  
  111. PROC wait() OF newPort
  112. ENDPROC WaitPort(self.mp)
  113.  
  114. PROC collect() OF newPort
  115. ENDPROC GetMsg(self.mp)
  116.  
  117. PROC collectlast() OF newPort
  118.    DEF msg, lastmsg=NIL:PTR TO newMsg
  119.    WHILE (msg:=self.collect())
  120.       IF lastmsg THEN lastmsg.reply()
  121.       lastmsg:=msg
  122.    ENDWHILE
  123. ENDPROC lastmsg
  124.  
  125. PROC getSigF() OF newPort
  126. ENDPROC Shl(1, self.mp.sigbit)
  127.  
  128.  
  129. ->----------SIGNALS---------------------
  130.  
  131. PROC waitSigs(signalnum=NIL) OF waitSigs
  132.    DEF signum
  133.    IF signum THEN signum := self.allocSigN(signalnum)
  134. ENDPROC signum
  135.  
  136. PROC allocSigN(signalnum) OF waitSigs
  137.    DEF signum
  138.    signum := AllocSignal(signalnum)
  139.    IF signum = -1 THEN RETURN -1
  140.    self.alloced_sigs := self.alloced_sigs OR Shl(1, signum)
  141. ENDPROC signum
  142.  
  143. PROC freeSigN(signalnum) OF waitSigs
  144.    DEF bits
  145.    bits := Shl(1, signalnum)
  146.    bits := Not(bits)
  147.    self.alloced_sigs := self.alloced_sigs AND bits
  148.    FreeSignal(signalnum)
  149. ENDPROC
  150.  
  151. PROC addSigF(sigf) OF waitSigs
  152.    self.added_sigs := self.added_sigs OR sigf
  153. ENDPROC
  154.  
  155. PROC remSigF(sigf) OF waitSigs
  156.    sigf := Not(sigf)
  157.    self.added_sigs := self.added_sigs AND sigf
  158. ENDPROC
  159.  
  160. PROC addSigN(signum) OF waitSigs
  161.    self.added_sigs := self.added_sigs OR Shl(1, signum)
  162. ENDPROC
  163.  
  164. PROC remSigN(signum) OF waitSigs
  165.    self.added_sigs := self.added_sigs AND Not(Shl(1, signum))
  166. ENDPROC
  167.  
  168. PROC wait() OF waitSigs
  169.    self.received_sigs := Wait(self.alloced_sigs OR self.added_sigs)
  170. ENDPROC self.received_sigs
  171.  
  172. PROC getSigF() OF waitSigs IS self.alloced_sigs OR self.added_sigs
  173.  
  174. PROC end() OF waitSigs
  175.    DEF sigbitcount=NIL, sig
  176.    WHILE self.alloced_sigs
  177.       sig:=Shl(1, sigbitcount)
  178.       IF sig AND self.alloced_sigs
  179.          FreeSignal(sigbitcount)
  180.          self.alloced_sigs := self.alloced_sigs AND Not(sig)
  181.       ENDIF
  182.       sigbitcount ++
  183.    ENDWHILE
  184. ENDPROC
  185.  
  186.